package com.garlic.face.jlf.ui;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.garlic.face.jlf.R;
import com.garlic.face.jlf.transaction.balance.CommonUtil;
import com.garlic.face.jlf.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;

public class SuperVisorDialog extends DialogFragment implements View.OnClickListener {
    private static final String TAG = "SuperVisorDialog";

    private String title;
    private String msg;
    private String hint;
    private TextView title_txt;
    private TextView msg_txt;
    public EditText mNumberEdit;
    private static SuperVisorDialog mSuperVisorDialog;
    private String _scancode;

    public void setCallBack(SuperVisorDialog.CallBack callBack) {
        this.callBack = callBack;
    }

    private SuperVisorDialog.CallBack callBack;
    public static SuperVisorDialog getInstance(String title,String msg, String hint){
        mSuperVisorDialog=new SuperVisorDialog();
        Bundle bundle=new Bundle();
        bundle.putString("title",title);
        bundle.putString("msg",msg);
        bundle.putString("hint",hint);
        mSuperVisorDialog.setArguments(bundle);
        return mSuperVisorDialog;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title=getArguments().getString("title");
            msg=getArguments().getString("msg");
            hint=getArguments().getString("hint");
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        CommonUtil.getInstance().hideNumber(getActivity(), mNumberEdit);
        super.onViewCreated(view, savedInstanceState);
        this.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent event) {
                if (event.getAction()==KeyEvent.ACTION_DOWN) {
                    if (event.getKeyCode()==KeyEvent.KEYCODE_ENTER){
                        LogUtils.d(TAG + "dispatchKeyEvent: KEYCODE_ENTER:", false);
                        LogUtils.d(TAG + "Main=" + _scancode, false);
                        _scancode = "";
                    }
                    else if(event.getUnicodeChar() == 0){
                    }
                    else if((char)event.getUnicodeChar() == '#'){
                        if(_scancode.startsWith("null")){
                            _scancode = _scancode.replace("null", "");
                        }
                        LogUtils.d(TAG + "dispatchKeyEvent: _scancode=" + _scancode, false);
                        EventBus.getDefault().post(_scancode);
                        _scancode = "";
                    }
                    else {
                        _scancode += (char)event.getUnicodeChar();
                        LogUtils.d(TAG + "Main=" + _scancode.toString(), false);

                    }
                }
                return false;
            }
        });
    }

    public void setMessages(String titleStr, String msgStr, String hintStr){
        title = titleStr;
        msg = msgStr;
        hint = hintStr;
        title_txt.setText(titleStr);
        msg_txt.setText(msgStr);
        mNumberEdit.setHint(hintStr);
    }

    public String getTitle(){
        return title;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        view=inflater.inflate(R.layout.popup_window_supervisor_login,container,false);
        title_txt=view.findViewById(R.id.popup_window_supervisor_title);
        msg_txt=view.findViewById(R.id.popup_window_supervisor_message);
        mNumberEdit = view.findViewById(R.id.popup_window_supervisor_in);
        ImageView btnClose = view.findViewById(R.id.supervisor_close);
        Button btnOk=view.findViewById(R.id.write_number_ok);
        Button btnDelete = view.findViewById(R.id.write_number_delete);
        Button btn0 = view.findViewById(R.id.write_number_0);
        Button btn1 = view.findViewById(R.id.write_number_1);
        Button btn2 = view.findViewById(R.id.write_number_2);
        Button btn3 = view.findViewById(R.id.write_number_3);
        Button btn4 = view.findViewById(R.id.write_number_4);
        Button btn5 = view.findViewById(R.id.write_number_5);
        Button btn6 = view.findViewById(R.id.write_number_6);
        Button btn7 = view.findViewById(R.id.write_number_7);
        Button btn8 = view.findViewById(R.id.write_number_8);
        Button btn9 = view.findViewById(R.id.write_number_9);
        title_txt.setText(title);
        msg_txt.setText(msg);
        mNumberEdit.setHint(hint);
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnOk.setOnClickListener(this);
        btnClose.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.popup_window_supervisor_login_layout:
                if (mNumberEdit.length() <= 0) { // 用户输入情况下保留
                    mSuperVisorDialog.dismiss();
                }
                break;
            case R.id.supervisor_layout:
                // 排除点击白框隐藏
                break;
            case R.id.supervisor_close:
                callBack.close_click(view);
                break;
            case R.id.write_number_0:
                mNumberEdit.append("0");
                break;
            case R.id.write_number_1:
                mNumberEdit.append("1");
                break;
            case R.id.write_number_2:
                mNumberEdit.append("2");
                break;
            case R.id.write_number_3:
                mNumberEdit.append("3");
                break;
            case R.id.write_number_4:
                mNumberEdit.append("4");
                break;
            case R.id.write_number_5:
                mNumberEdit.append("5");
                break;
            case R.id.write_number_6:
                mNumberEdit.append("6");
                break;
            case R.id.write_number_7:
                mNumberEdit.append("7");
                break;
            case R.id.write_number_8:
                mNumberEdit.append("8");
                break;
            case R.id.write_number_9:
                mNumberEdit.append("9");
                break;
            case R.id.write_number_ok:
                callBack.ok_click(view);
                break;
            case R.id.write_number_delete:
                callBack.delete_click(view);
                break;
            default:
                break;
        }
    }

    public  interface CallBack{
        void ok_click(View view);
        void delete_click(View view);
        void close_click(View view);
    }

}
